<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

require '../autoload.php';

use Lg\View\View;
use Lg\Component\Paginator;
use Lg\Http\Input;
use Lg\Component\Categories;

//初始化
function initialize() {
    screen()->addBreadcrumb(__('分类管理'), url_for('/node/category.php'), '', false);
}

//分類列表
function indexAction() {
//    $cat = new \Lg\Component\SimpleCategories('menu', 'menu_language', 'menu_id');
//    echo $cat->toUL($cat->buildMenu());
//    die;
    $paginator = new Paginator(Input::get('page', 1), Input::get('limit', 30), Input::query());
    $filter['offset'] = $paginator->getOffset();
    $filter['pagesize'] = $paginator->getLimit();
    $filter['depth'] = 1; //分類深度
    $category = new Categories();
    $total_rows = $category->getTotalCategory($filter); //總行數
    $categories = $category->getCategory($filter); //获取分类
    $paginator->setTotal($total_rows); //设置分页组件总行数
    screen()->setTitle(__('分类管理'), __('')); //设置页面标题
    return View::make('category', [
                'categories' => $categories,
                'total_rows' => $total_rows,
                'paginator' => $paginator
    ]);
}

//添加分类
function addAction() {
    if (Input::isMethod('post')) {
        $data = Input::post('category', []);
        if (!isset($data['parent_id']) || empty($data['parent_id'])) {
            $data['parent_id'] = 1;
        } else {
            $data['parent_id'] = intval($data['parent_id']);
        }
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $data['sort_order'] = 1;
        $data['languages'] = Input::post('language', []);
        $category = new Categories;
        $category->addCategory($data);
        return redirect('/node/category.php')->withFlash('success', '分类添加成功');
    }
    $category = new Categories();
    $categories = $category->getCategory(['depth' => 1]);
    screen()->setTitle(__('添加分类'), __('分类管理'));
    return View::make('category-form', [
                'categories' => $categories,
                'languages' => \h1cms\model\Language::getAllAvailables()
    ]);
}

//编辑分类
function editAction() {
    $category_id = intval(Input::get('id'));
    if (Input::isMethod('post')) {
        $data = Input::post('category', []);
        if (!isset($data['parent_id']) || empty($data['parent_id'])) {
            $data['parent_id'] = 1;
        } else {
            $data['parent_id'] = intval($data['parent_id']);
        }
        $data['updated_at'] = time();
        $data['languages'] = Input::post('language', []);

        $category = new Categories;
        //删除所有语言
        \h1cms\model\CategoryLanguage::delete($category_id);
        $category->updateCategory($category_id, $data);
        return redirect('/node/category.php')->withFlash('success', '分类添加成功');
    }
    $category = new Categories;
    $categories = $category->getCategory(['depth' => 1]);
    screen()->setTitle(__('修改分类'), __('分类管理'));
    return View::make('category-form',
                    [
                'categories' => $categories,
                'category' => h1cms\model\Category::findById($category_id),
                'category_languages' => h1cms\model\Category::getAllLanguages($category_id),
                'languages' => \h1cms\model\Language::getAllAvailables()
    ]);
}

//删除分类
function deleteAction() {
    $category_id = intval(Input::get('id'));
    $category = new Categories;
    $category->removeCategory($category_id);
    return redirect('/node/category.php')->withFlash('success', '分类删除成功');
}
